package com.sunday.scoremanager.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.widgets.EmptyLayout;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.scoremanager.R;
import com.sunday.scoremanager.adapter.ProductAdapter;
import com.sunday.scoremanager.adapter.StaffAccountAdapter;
import com.sunday.scoremanager.entity.Product;
import com.sunday.scoremanager.entity.StaffAccount;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;



/**
 * Created by 刘涛 on 2017/10/26.
 */

public class ProductManageActivity extends BaseActivity {


    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private List<Product> dataSet=new ArrayList<>();
    private ProductAdapter adapter;
    private LinearLayoutManager layoutManager;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_list);
        ButterKnife.bind(this);
        titleView.setText("产品管理");
        rightBtn.setVisibility(View.VISIBLE);
        rightBtn.setImageResource(R.mipmap.add);
        initRefreshLayout();
    }

    private void initRefreshLayout(){
        emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new ProductAdapter(mContext, dataSet);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext).drawable(R.drawable.shape_divider_width).build());
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getData();
            }
        });
        emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }
        });
        getData();
    }

    private void getData(){
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        dataSet.clear();
        for (int i=0;i<10;i++){
            dataSet.add(new Product());
        }
        adapter.notifyDataSetChanged();
    }

    @OnClick(R.id.right_btn)
    void add(){
        intent=new Intent(mContext,AddProductActivity.class);
        startActivity(intent);
    }
}
